<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
        <el-form-item label="创建时间">
          <el-date-picker
                  v-model="dateRange"
                  size="small"
                  style="width: 240px"
                  value-format="yyyy-MM-dd"
                  type="daterange"
                  range-separator="-"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增招生专业</el-button>
        </el-col>

      </el-row>
  
      <el-table ref="uiTable" v-loading="loading" :data="subjList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column type="index" label="序号" width="50"/>
        <el-table-column label="科目名称" align="center" prop="majorName" />
        <el-table-column label="排序" align="center" prop="sort" />
        <el-table-column label="创建时间" align="center" prop="createTime" >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        
        <el-table-column label="操作" align="center" >
          <template slot-scope="scope">
            <el-button
                    size="mini"
                    circle type="primary" icon="el-icon-edit"
                    @click="handleUpdate(scope.row)"
                    title="修改"
            />
            <el-button
                    size="mini"
                    circle type="danger" icon="el-icon-delete"
                    @click="handleDelete(scope.row)"
                    title="删除"
            />
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改参数配置对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="70%">
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-form-item label="招生专业名称" prop="majorName" >
            <el-input type="input"  v-model="form.majorName" placeholder="请输入专业名称" />
          </el-form-item>
          <el-form-item label="专业备注" >
            <el-input type="input"  v-model="form.notes" placeholder="请输入专业名称" />
          </el-form-item>
          <el-form-item label="排序" prop="sort" >
            <el-input-number v-model="form.sort" ></el-input-number>
          </el-form-item>
          <el-form-item label="专业图片:" prop="majorPicture">
                        <el-upload border style="width: 120px;"
                            class="avatar-uploader"
                            :action="activeUrl"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess"
                            :before-upload="beforeAvatarUpload">
                            <img v-if="form.majorPicture" :src="imgPath + form.majorPicture" class="avatar">
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
      </el-form-item>
          <el-form-item label="招生专业简介" prop="recruitSynopsis">
            <editor :value="form.recruitSynopsis" @input="updateContext" :options="toolbarOptions"></editor>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import {getList, addOrUpdate, deleteById} from "@/api/workmgr/recruitMajor.js";
  import  editor  from '@/components/Editor/index'
  
  export default {
    name: "recruitMajor",
    components: {editor},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        indexs:[],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 科目表格数据
        subjList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 状态数据字典
        statusOptions: [],
        // 日期范围
        dateRange: [],
            //图片路径
        imgPath: process.env.VUE_APP_BASE_API + '/major/picture/',
        //上传的路径
        activeUrl: '',
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: undefined,
          type: undefined,
          status: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
            majorName: [
                { required: true, message: "专业名称不能为空", trigger: "blur" }
            ],
            recruitSynopsis:[
                { required: true, message: "专业简介不能为空", trigger: "blur" }
            ]
        
        },
        toolbarOptions : [
            ["bold", "italic", "underline", "strike"],       // 加粗 斜体 下划线 删除线
            ["blockquote", "code-block"],                    // 引用  代码块
            [{ list: "ordered" }, { list: "bullet" }],       // 有序、无序列表
            [{ indent: "-1" }, { indent: "+1" }],            // 缩进
             [{ size: ["small", false, "large", "huge"] }],   // 字体大小
            [{ header: [1, 2, 3, 4, 5, 6, false] }],         // 标题
            [{ color: [] }, { background: [] }],             // 字体颜色、字体背景颜色
            [{ align: [] }],                                 // 对齐方式
            ["clean"],                                       // 清除文本格式
            ["link", "image", "video"],                       // 链接、图片、视频
            ['formula']
        ]
      };
    },
    created() {
      this.getList();
      /*this.getDicts("t_sys_normal_disable").then(response => {
        this.statusOptions = response.data;
      });*/
      this.activeUrl = process.env.VUE_APP_BASE_API +'/workmgr/major/uploadImage';
    },
    methods: {
        updateContext(val){
            this.form.recruitSynopsis = val
        },
      /** 查询字典类型列表 */
      getList() {
        this.loading = true;
        getList(this.queryParams).then(response => {
            this.subjList = response.data.list;
            this.total = response.data.total;
            this.loading = false;
          }
        );
      },
      
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          recruitSynopsis: undefined,
          majorName: undefined,
          sort: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加科目";
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
  
        this.indexs = [];
        selection.forEach((val, index) => {
          this.subjList.forEach((v, i) => {
            // id 是每一行的数据id
            if(val.id == v.id){
              console.log(i+1);
              this.indexs.push(i+1);
            }
          })
        }) ;
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.form = row;
        this.open = true;
        
      },
      handleSocre(row) {
        this.$router.push({path:"/teacher/evaluation/mytask/taskScoreData", query:{teutId: row.teutId}})
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
                addOrUpdate(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.message);
                }
              });
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$confirm('是否确认删除?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            return deleteById(row);
          }).then(() => {
            this.getList();
            this.msgSuccess("删除成功");
          }).catch(function() {});
      },

      //上传专业照片成功回调
      handleAvatarSuccess(res, file) {
        // this.form.tenantLogo = "http://127.0.0.1:8088" + '/school/file/' + res.result; 
        this.form.majorPicture = res.result; 
        this.$forceUpdate();
    },
      //上传专业照片前校验
      beforeAvatarUpload(file) {
        const isLt2M = file.size / 1024 / 1024 < 10;
        if (!isLt2M) {
            this.$message.error('上传专业照片大小不能超过10MB!');
        }
        return isLt2M;
    }
      
  
    }
  };
  </script>

<style lang='scss' scoped>
/deep/ .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}
.avatar {
  width: 120px;
  height: 120px;
  display: block;
}
.avatar1 {
  width: 120px;
  height: 120px;
  display: block;
}

</style>
  